package com.yhsoft.bill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.bill.controller.request.SysHkInVo;
import com.yhsoft.bill.controller.request.SysSkInVo;
import com.yhsoft.bill.controller.request.SysYszkMxInVo;
import com.yhsoft.bill.controller.response.SysYszkMxOutVo;
import com.yhsoft.bill.entity.SysYszkMx;
import com.yhsoft.bill.mapper.SysYszkMxMapper;
import com.yhsoft.bill.service.ISysYszkMxService;
import com.yhsoft.common.service.TokenService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * <p>
 * 平台应收账款详情 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-11-03
 */
@Service
public class SysYszkMxServiceImpl extends ServiceImpl<SysYszkMxMapper, SysYszkMx> implements ISysYszkMxService {

    @Override
    public IPage<SysYszkMxOutVo> getYszkMx(SysYszkMxInVo inVo, Integer companyId, Long pageNo, Long pageSize) {
        LambdaQueryWrapper<SysYszkMx> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysYszkMx::getCompanyId, companyId);
        queryWrapper.like(StringUtils.hasLength(inVo.getPayBy()), SysYszkMx::getPayBy, inVo.getPayBy());
        queryWrapper.eq((inVo.getIsPay() != null), SysYszkMx::getIsPay, inVo.getIsPay());
        queryWrapper.ge(inVo.getStartTime() != null, SysYszkMx::getPayTime, inVo.getStartTime());
        if (inVo.getEndTime() != null) {
            queryWrapper.le(SysYszkMx::getPayTime, LocalDateTime.of(inVo.getEndTime(), LocalTime.MAX));
        }
        queryWrapper.orderByDesc(SysYszkMx::getCreateTime);
        Page<SysYszkMx> page = new Page<>(pageNo, pageSize);
        IPage<SysYszkMx> iPage = baseMapper.selectPage(page, queryWrapper);

        IPage<SysYszkMxOutVo> convert = iPage.convert(SysYszkMx -> {
            SysYszkMxOutVo form = new SysYszkMxOutVo();
            BeanUtils.copyProperties(SysYszkMx, form);
            return form;
        });
        return convert;
    }

    @Override
    public String sk(SysSkInVo inVo) {
        SysYszkMx yszkMx = new SysYszkMx();
        yszkMx.setCompanyId(inVo.getCompanyId());
        yszkMx.setPayBy(TokenService.getUserBytoken().getName());
        yszkMx.setType(inVo.getType());

        // 判断类型，类型 1-充值，2-临时授信
        // 累计充值 = 普通充值的和，加上临时授信还款的和
        if(inVo.getType().equals(1)){
            yszkMx.setIsPay(1);
        }else {
            yszkMx.setIsPay(0);
        }

        yszkMx.setPayAmount(inVo.getPayAmount());
        yszkMx.setVoucher(inVo.getVoucher());
        yszkMx.setPayTime(LocalDateTime.now());
        this.baseMapper.insert(yszkMx);
        return "success";
    }

    @Override
    public String hk(SysHkInVo inVo) {
        SysYszkMx yszkMx = this.baseMapper.selectById(inVo.getId());
        if(yszkMx == null){
            return "还款失败！未查询到应收账款详情记录！";
        }

        // 还款只允许同一经手人操作
        if(!yszkMx.getPayBy().equals(TokenService.getUserBytoken().getName())){
            return "还款失败！您不是经手人！";
        }

        // 判断类型，类型 1-充值，2-临时授信
        if(yszkMx.getType() == 1){
            return "还款失败！类型为充值，无需还款！";
        }

        // 判断类型，类型 1-充值，2-临时授信
        if(yszkMx.getIsPay() == 1){
            return "还款失败！不能重复还款！";
        }

        // 还款
        yszkMx.setVoucher(inVo.getVoucher());
        yszkMx.setIsPay(1);
        this.baseMapper.updateById(yszkMx);
        return "success";
    }
}
